<?php

App::uses('AppController', 'Controller');

/**
 * Events Controller
 *
 * @property Sync $Sync
 * @property Chapter $Chapter
 */
class SyncsController extends AppController {
    
    public $helpers = array('Form');
    
    public $scaffold = 'admin';
    
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->deny(array('admin_index', 'admin_view', 'admin_add', 'admin_edit', 'admin_delete'));
    }
    
    
    
    /**
     * admin_add method
     *
     * @return void
     */
    public function admin_add() {
        if ($this->request->is('post')) {
            
            if($this->request->data['Sync']['Chapter']=='0'){
                unset($this->request->data['Sync']['Chapter']);
                $this->request->data['Sync']['chapter_id'] = null;
            } else {
                $this->request->data['Sync']['chapter_id'] = $this->request->data['Sync']['Chapter'];
            }
            $this->Sync->create();
            if ($this->Sync->saveAll($this->request->data)) {
                $this->Session->setFlash(__('The sync data has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The synch could not be saved. Please, try again.'));
            }
        }
        $chapters = $this->Sync->Chapter->find('list');
        $chapters['0'] = 'none';
        $this->set(compact('chapters'));
    }
    
    
    public function admin_edit($id = null) {
        $this->Sync->id = $id;
        if (!$this->Sync->exists()) {
            throw new NotFoundException(__('Invalid event'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Sync->save($this->request->data)) {
                $this->Session->setFlash(__('The sync has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The sync could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->Sync->read(null, $id);
        }
        $chapters = $this->Sync->Chapter->find('list');
        $chapters['0'] = 'none';
        $this->set(compact('chapters'));
    }
    
}
